﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Configuration;

namespace YasharEl.Infrastructure.ExceptionHandling.Configuration
{
    public partial class ExceptionHandlerCollection
    {
        public static Dictionary<string, Type> ExceptionHandlersSubclassesMap = new Dictionary<string, Type>();

        static ExceptionHandlerCollection()
        {
            // TODO : Add new exception handler config here!!!
            ExceptionHandlersSubclassesMap.Add("loggingExceptionHandlerConfig", typeof(LoggingExceptionHandlerConfig));
            ExceptionHandlersSubclassesMap.Add("faultContractExceptionHandlerConfig", typeof(FaultContractExceptionHandlerConfig));
            ExceptionHandlersSubclassesMap.Add("replaceExceptionHandlerConfig", typeof(ReplaceExceptionHandlerConfig));
            ExceptionHandlersSubclassesMap.Add("wrapExceptionHandlerConfig", typeof(WrapExceptionHandlerConfig));
        }

        protected virtual bool HandleUnrecognizedElement(string elementName, XmlReader reader)
        {
            try
            {
                if (ExceptionHandlersSubclassesMap.ContainsKey(elementName))
                {
                    Type exceptionHandlerConfigType = ExceptionHandlersSubclassesMap[elementName];

                    ExceptionHandlerConfig currentElement = (ExceptionHandlerConfig)Activator.CreateInstance(exceptionHandlerConfigType);

                    currentElement.DeserializeElement(reader);

                    BaseAdd(currentElement, true);

                    return true;
                }
            }
            catch
            {
                return false;
            }

            return false;
        }
    }
}
